package controller.Actions;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import controller.Forms.SearchAccountantForm;
import dao.AccountantDAO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Accountant;
import model.Erro;
import model.User;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Adriano
 */
public class BuscaContadorAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {

            User u = (User) request.getSession().getAttribute("User");
//            SearchAccountantForm bcf = (SearchAccountantForm)form;
//            Accountant contador = new Accountant();
//            contador.setTxtCpf(bcf.getTxtCpf());
//            
//            AccountantDAO cDao = new AccountantDAO();
//            contador = (Accountant) cDao.find(contador);
//            
//            cDao.closeDAO();
            
            if (u == null) {
                request.getSession().setAttribute("ErroMSG", Erro.erroMsg[Erro.NAO_AUTENTICADO]);
                return mapping.findForward("erroAlias");
            }else {
            SearchAccountantForm bcf = (SearchAccountantForm) form;
            Accountant contador = new Accountant();
            contador.setTxtCpf(bcf.getTxtCpf().replace(".", "").replace("-", ""));

            AccountantDAO cDao = new AccountantDAO();
            contador = (Accountant) cDao.find(contador);

//            cDao.closeDAO();
//
//            if (contador == null) {
//                request.getSession().setAttribute("ErroMSG", Erro.erroMsg[Erro.DADOS_INVÁLIDOS]);
//                return mapping.findForward("erroAlias");
//            } else {
                request.getSession().setAttribute("Contador", contador);
                return mapping.findForward("ResultSearchAccountantAlias");
            }

        } catch (Exception e) {
            System.err.println("BUSCACONTADORACTION - " + e.getMessage());
            return mapping.findForward("erroAlias");
        }
    }

}
